package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    JedisPool jedisPool;


    @Reference
    SetmealService setmealService;

    /**
     * 1. 生成唯一的文件名（uuid）
     * 2. 截取截取源文件的后缀名
     * 3. 上传七牛云
     * @param imgFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestBody MultipartFile imgFile){
//        1. 生成唯一的文件名（uuid）
        String uuid = UUID.randomUUID().toString().replace("-", "");
//        2. 截取截取源文件的后缀名
        //获取原始文件名
        String originalFilename = imgFile.getOriginalFilename();
        //文件名中 最后一次出现 . 的索引
        int index = originalFilename.lastIndexOf(".");
        //截取字符串
        String extendName = originalFilename.substring(index);

        //最终在服务器上的文件名
        String serverFileName = uuid + extendName;
        System.out.println(serverFileName);
//        3. 上传七牛云
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), serverFileName);
            //已经上传成功， 将图片的名称存储在redis中
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,serverFileName);
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS, serverFileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }
    @RequestMapping("/add")
    public Result add(Integer[] checkgroupIds, @RequestBody Setmeal setmeal){
        try {
            setmealService.add(checkgroupIds,setmeal);
            return  new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmealService.findByPage(queryPageBean);
        //判断是否有条件， 是否查询到了数据
        if(queryPageBean.getQueryString() != null  && pageResult.getRows().size() == 0){
            //重新查询第一页
            queryPageBean.setCurrentPage(1);
            pageResult = setmealService.findByPage(queryPageBean);
        }
        return pageResult;
    }

}
